package com.itstherules.jmsunit.expectation;

import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.TextMessage;

import com.itstherules.jmsunit.expectation.JmsExpectation;
import com.itstherules.jmsunit.expectation.StringExpectation;


public final class JmsMessageAndStringExpectation implements JmsExpectation {

	private final StringExpectation expectation;

	public JmsMessageAndStringExpectation(StringExpectation expectation) {
		this.expectation = expectation;
	}
	
	public void compute(Message actual) throws AssertionError {
		try {
			if(!(actual instanceof TextMessage)) { 
				throw new AssertionError("Expected a Jms TextMessage but was a " + actual.getClass().toString()); 
			}
			expectation.compute(((TextMessage) actual).getText());
		} catch (JMSException e) {
			throw new AssertionError("Unable to read text from message: " + e);
		}
	}

}
